<?php
include_once('../config.php'); 
	session_start();
	LinkDataBase();
			
	$Array_DBTableName = DBTableNameGet($_SESSION['ID'], $_POST['year'], $_POST['season'], $_POST['tablename']);
	$filename = $Array_DBTableName[0];
	
	//Find question.
	$sql = "select * from ".$filename." LEFT JOIN sheet ON ".$filename.".table_name = binary(sheet.table_name) 
			WHERE table_number='".$_POST['tablename']."' AND 
			item NOT LIKE '%Base: All Respondents%' AND 
			item NOT LIKE '%Base: Respondents%'";
	
	$result= mysql_query($sql);
	$numResult = mysql_num_rows($result);
			
	if ($numResult > 0) {
?>
<script type='text/javascript'>
		var data = new google.visualization.DataTable();
		var myrow = new Array();
			
		data.addColumn('string', 'string');
		data.addColumn('number', 'percent');
		<?php
		$item = "";
		while($row = mysql_fetch_assoc($result)) {
			if ((($item!="")&&($row['item']!='Sigma')&&
				(strpos($row['total'],'%'))&&
				($_POST['keyword']==''))||
				//For keyword exists.
				(($item!="")&&($row['item']!='Sigma')&&
				(strpos($row['total'],'%'))&&
				($_POST['keyword']!='')&&
				//contain is YES.
				(((strpos($item,(string)$_POST['keyword']))&&
				($_POST['contain']=='YES'))||
				//contain is NO.
				((!strpos($item,(string)$_POST['keyword']))&&
				($_POST['contain']=='NO'))))) {
		?>			
				var value = <?php echo str_replace('%','',$row['total']); ?>;
				myrow.push(['<?php echo str_replace("'","\'",str_replace('(Net)','',$item)); ?>', value]);			
		<?php
			}
			else if ($row['item']=='Sigma')
				break;
							
			$item = $row['item'];
		}
		?>
						
        // A column for custom tooltip content
		data.addRows(myrow);

    	var options = {
			chartArea: {top:0, left:10, width: 300, height: '100%'},
			width: '100%',
			tooltip: {text: 'percentage', showColorCode: 'true'}
		};
	
		var chart = new google.visualization.PieChart(document.getElementById('<?php echo $_POST['season'].$_POST['tablename']; ?>'));
    	chart.draw(data, options);
</script>
<?php
	}
?>
<div id='<?php echo $_POST['season'].$_POST['tablename']; ?>' style="width: 100%;"></div>